package persistency.reader;

/**
 * CourseReader.java
 * Purpose: this class represents a course that students can take
 * 
 * @author Hans Brems
 * @version 1.0 05/11/2011
 */

import java.util.ArrayList;

import model.CourseHistory;
import model.Student;
import model.container.CourseContainer;
import model.container.StudentContainer;

public class CourseHistoryReader extends ContentReader {
	
	/** Constructs a course reader object **/
	public CourseHistoryReader() {
	}
	
	public CourseHistoryReader(String filePath) {
		super(filePath);
	}
	
	/** Reads a text file containing course objects into a collection */
	public ArrayList<CourseHistory> read() {
		
		// Create list
		ArrayList<CourseHistory> courseHistoryElements = new ArrayList<CourseHistory>();
		
		// Skip the first line (header)
		this.scanner.nextLine();

		// Construct a course object from each line
		while (this.scanner.hasNext()) {
			
			// Create a new course
			CourseHistory courseHistory = new CourseHistory();
			
			// Set course values
			
			Student student = StudentContainer.getInstance().getStudentByID(this.scanner.next());
			student.getCourseHistories().add(courseHistory);
			int courseCode = Integer.parseInt(this.scanner.next());
			courseHistory.setCourse(CourseContainer.getInstance().getCourseByID(courseCode));
			courseHistory.setSchoolYear(this.scanner.next());
			courseHistory.setScore(Integer.parseInt(this.scanner.next()));
			
			// Add course to collection
			courseHistoryElements.add(courseHistory);
		}
		
		// Return list
		return courseHistoryElements;
	}
}